package jp.co.dailyReport.weeklyReport.sql;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.weeklyReport.WeeklyReportWorkItem;

/**
 * 週報作業項目情報を取得するクラスです。
 */
public class WeeklyReportWorkItemLoadExecuter implements Executer {

	private static Logger log = Logger.getLogger(WeeklyReportWorkItemLoadExecuter.class);
	private int weeklyReportId;

	public WeeklyReportWorkItemLoadExecuter(int weeklyReportId) {
		this.weeklyReportId = weeklyReportId;
	}

	/**
	 * 週報IDを基に、週報作業項目情報を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return 週報作業項目オブジェクトの配列
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select * from weeklyReportWorkItem where weeklyReportId=?";
		log.debug("sql: " + sql);
		Vector answer = new Vector();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, weeklyReportId);
			rset = pstmt.executeQuery();
			WeeklyReportWorkItem weeklyReportWorkItem;
			while(rset.next()) {
				weeklyReportWorkItem = new WeeklyReportWorkItem();
				weeklyReportWorkItem.setWeeklyReportWorkItemId(rset.getInt("weeklyReportWorkItemId"));
				weeklyReportWorkItem.setWorkItemId(rset.getInt("workItemId"));
				weeklyReportWorkItem.setWeeklyReportId(rset.getInt("weeklyReportId"));
				weeklyReportWorkItem.setWorkingHours(rset.getDouble("workingHours"));
				weeklyReportWorkItem.setDelFlg(Utilities.decimalToBoolean(rset.getInt("delFlg")));
				weeklyReportWorkItem.setUpDay(Utilities.getCalendar(rset.getDate("upDay")));
				answer.add(weeklyReportWorkItem);
			}
		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (WeeklyReportWorkItem[])answer.toArray(new WeeklyReportWorkItem[0]);
	}

}
